package com.maoslv.controller;

import com.maoslv.aop.LogAnnotation;
import com.maoslv.dto.Result;
import com.maoslv.pojo.Order;
import com.maoslv.pojo.Pagination;
import com.maoslv.service.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Scope("request")
public class OrderController {
    @Autowired
    OrderService orderService;

    @LogAnnotation(operator = "添加订单")
    @PostMapping("/add")
    public Result addOrder(@RequestBody Order order) {
        if (orderService.addOrder(order) > 0) {
            return Result.succ("添加成功", null);
        }
        return Result.fail("添加失败");
    }

    @LogAnnotation(operator = "获取订单列表")
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('order:list')")
    public Result getOrderList(@RequestBody Pagination pagination) {
        Map<String,Object> result = orderService.getOrderList(pagination);
        if (result != null) {
            return Result.succ("获取成功",result);
        }
        return Result.fail("获取失败");
    }

    @LogAnnotation(operator = "审核订单")
    @GetMapping("update/{id}/{examine}")
    @PreAuthorize("hasAuthority('order:update')")
    public Result updateOrder(@PathVariable Integer id, @PathVariable Integer examine) {
        if (orderService.updateOrder(id, examine) > 0) {
            return Result.succ("修改成功", null);
        }
        return Result.fail("修改失败");
    }
}
